/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bean.lazy.LazyHumanWithActorsDataModel;
import org.ipti.br.tag.bo.ActorBO;
import org.ipti.br.tag.bo.ActorScheduleBO;
import org.ipti.br.tag.bo.AdditionalInfoBO;
import org.ipti.br.tag.bo.DisciplineBO;
import org.ipti.br.tag.bo.HumanBO;
import org.ipti.br.tag.bo.OrganizationBO;
import org.ipti.br.tag.bo.PersonageBO;
import org.ipti.br.tag.bo.PlanBO;
import org.ipti.br.tag.bo.SeasonBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.bo.UnityScheduleBO;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.AdditionalInfo;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Organization;
import org.ipti.br.tag.entity.Personage;
import org.ipti.br.tag.entity.Unity;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author israel
 */
@Named(value = "enrollment")
@SessionScoped
public class EnrollmentBean extends AppBaseBean {

    private Long personageID;
    private Long organizationID;
    private Long unityID;
    private Long humanID;
    private Integer year;
    private String humanName;
    private Personage personage;
    private Human human;
    private Human humanToRemove;
    private List<Human> enrollmentHumanList;
    private List<SelectItem> organizationList;
    private List<SelectItem> yearList;
    private List<SelectItem> unityList;
    @Inject
    private OrganizationBO organizationBO;
    @Inject
    private DisciplineBO disciplineBO;
    @Inject
    private SeasonBO seasonBO;
    @Inject
    private PlanBO planBO;
    @Inject
    private PersonageBO personageBO;
    @Inject
    private HumanBO humanBO;
    @Inject
    private ActorBO actorBO;
    @Inject
    private UnityBO unityBO;
    @Inject
    private AdditionalInfoBO additionalInfoBO;
    @Inject
    private UnityScheduleBO unityScheduleBO;
    @Inject
    private ActorScheduleBO actorScheduleBO;
    @Inject
    private SessionBean sessionBean;
    private boolean success;
    private final int INITIAL_YEAR = 1986;
    private LazyHumanWithActorsDataModel possibleHumans;

    public EnrollmentBean() {
    }

    public int calendarYear(Calendar calendar) {
        return calendar.get(Calendar.YEAR);
    }

    public void init() {
        personage = personageBO.find(personageID);
        
        enrollmentHumanList = new ArrayList<Human>();
        possibleHumans = new LazyHumanWithActorsDataModel(humanBO.getHumanDAO(),
                unityBO.unityChildrenPerOrganization2(sessionBean.getLoggedActor().getUnity().getId(), personage.getOrganization().getId()),
                personageID);

        loadOrganizationList();
        unityList = null;
    }

    public void enroll() {
        try {
            Unity unityAux = unityBO.find(unityID);
            Actor actorAux;
            if(enrollmentHumanList.isEmpty()){
                addErrorMessage("enrollmentForm", "Nenhum aluno foi selecionado para a matrícula.", null);
            }else{
                for (Human humanAux : enrollmentHumanList) {
                    actorAux = new Actor(unityAux.getActivated(), unityAux.getDeactivated(), humanAux, unityAux, personageBO.getPersonage(unityAux.getOrganization().getId(), personageID));
                    actorBO.create(actorAux);
                    actorBO.getActorDAO().flush();
                }
                addInfoMessage("enrollmentForm", "Operação realizada com sucesso", null);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            addErrorMessage("enrollmentForm", "Um erro inesperado foi encontrado", null);
        }
        init();
    }
    
    public void addHuman() {
        if (enrollmentHumanList == null) {
            enrollmentHumanList = new ArrayList<Human>();
        }
        if (human != null) {
            boolean alreadyAdded = false;
            for (Human humanAux : enrollmentHumanList) {
                if (human.getId().longValue() == humanAux.getId().longValue()) {
                    alreadyAdded = true;
                    break;
                }
            }
            if (!alreadyAdded) {
                enrollmentHumanList.add(human);
            } else {
                addErrorMessage("enrollmentForm:humanTable", "Já foi adicionado.", null);
            }
        }
    }

    public void removeHuman(Long id) {
        for (Human humanAux : enrollmentHumanList) {
            if (humanAux.getId().toString().equals(id.toString())) {
                enrollmentHumanList.remove(humanAux);
                break;
            }
        }
    }

    public void loadYearList() {
        unityList = new ArrayList<SelectItem>();
        unityID = null;

        yearList = new ArrayList<SelectItem>();
        year = null;
        yearList.add(new SelectItem(null, "Selecione um Ano"));
        for (int i = Calendar.getInstance().get(Calendar.YEAR); i >= INITIAL_YEAR; i--) {
            yearList.add(new SelectItem(i, String.valueOf(i)));
        }
    }

    public void loadOrganizationList() {
        organizationID = null;
        unityList = new ArrayList<SelectItem>();
        unityID = null;
        yearList = new ArrayList<SelectItem>();
        year = null;

        organizationList = new ArrayList<SelectItem>();
        organizationList.add(new SelectItem(null, "Selecione uma Organização"));

        for (Organization o : organizationBO.getOrganizationsByPersonages(personageBO.getPersonageChildrenIDs(personageID))) {
            organizationList.add(new SelectItem(o.getId(), o.getName()));
        }
    }

    public void loadUnityList() {
        unityList = new ArrayList<SelectItem>();
        unityList.add(new SelectItem(null, "Selecione uma unidade"));
        for (Unity u : unityBO.unitiesPerID(
            unityBO.unityActivatedChildren(sessionBean.getLoggedActor().getUnity().getId(), organizationID, year))) {
            unityList.add(new SelectItem(u.getId(), u.getName()));
        }
    }

    public List<AdditionalInfo> personAdditionalInfo(Long personID) {
        return additionalInfoBO.findAddInfosPerPerson(personID);
    }

    public String getHumanName() {
        return humanName;
    }

    public void setHumanName(String humanName) {
        this.humanName = humanName;
    }

    public List<SelectItem> getOrganizationList() {
        if (organizationList == null) {
            loadOrganizationList();
        }
        return organizationList;
    }

    public void setOrganizationList(List<SelectItem> organizationList) {
        this.organizationList = organizationList;
    }

    public List<SelectItem> getUnityList() {
        if (unityList == null) {
            unityList = new ArrayList<SelectItem>();
        }
        return unityList;
    }

    public void setUnityList(List<SelectItem> unityList) {
        this.unityList = unityList;
    }

    public List<SelectItem> getYearList() {
        if (yearList == null) {
            loadYearList();
        }
        return yearList;
    }

    public void setYearList(List<SelectItem> yearList) {
        this.yearList = yearList;
    }

    public DisciplineBO getDisciplineBO() {
        return disciplineBO;
    }

    public void setDisciplineBO(DisciplineBO disciplineBO) {
        this.disciplineBO = disciplineBO;
    }

    public OrganizationBO getOrganizationBO() {
        return organizationBO;
    }

    public void setOrganizationBO(OrganizationBO organizationBO) {
        this.organizationBO = organizationBO;
    }

    public PlanBO getPlanBO() {
        return planBO;
    }

    public void setPlanBO(PlanBO planBO) {
        this.planBO = planBO;
    }

    public SeasonBO getSeasonBO() {
        return seasonBO;
    }

    public void setSeasonBO(SeasonBO seasonBO) {
        this.seasonBO = seasonBO;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Long getPersonageID() {
        return personageID;
    }

    public void setPersonageID(Long personageID) {
        this.personageID = personageID;
    }

    public Personage getPersonage() {
        return personage;
    }

    public void setPersonage(Personage personage) {
        this.personage = personage;
    }

    public PersonageBO getPersonageBO() {
        return personageBO;
    }

    public void setPersonageBO(PersonageBO personageBO) {
        this.personageBO = personageBO;
    }

    public ActorBO getActorBO() {
        return actorBO;
    }

    public void setActorBO(ActorBO actorBO) {
        this.actorBO = actorBO;
    }

    public HumanBO getHumanBO() {
        return humanBO;
    }

    public void setHumanBO(HumanBO humanBO) {
        this.humanBO = humanBO;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public UnityBO getUnityBO() {
        return unityBO;
    }

    public void setUnityBO(UnityBO unityBO) {
        this.unityBO = unityBO;
    }

    public AdditionalInfoBO getAdditionalInfoBO() {
        return additionalInfoBO;
    }

    public void setAdditionalInfoBO(AdditionalInfoBO additionalInfoBO) {
        this.additionalInfoBO = additionalInfoBO;
    }

    public Long getOrganizationID() {
        return organizationID;
    }

    public void setOrganizationID(Long organizationID) {
        this.organizationID = organizationID;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public Long getUnityID() {
        return unityID;
    }

    public void setUnityID(Long unityID) {
        this.unityID = unityID;
    }

    public Human getHuman() {
        return human;
    }

    public void setHuman(Human human) {
        this.human = human;
    }

    public List<Human> getEnrollmentHumanList() {
        if (enrollmentHumanList == null) {
            enrollmentHumanList = new ArrayList<Human>();
        }
        return enrollmentHumanList;
    }

    public void setEnrollmentHumanList(List<Human> enrollmentHumanList) {
        this.enrollmentHumanList = enrollmentHumanList;
    }

    public Human getHumanToRemove() {
        return humanToRemove;
    }

    public void setHumanToRemove(Human humanToRemove) {
        this.humanToRemove = humanToRemove;
    }

    public Long getHumanID() {
        return humanID;
    }

    public void setHumanID(Long humanID) {
        this.humanID = humanID;
    }

    public LazyHumanWithActorsDataModel getPossibleHumans() {
        return possibleHumans;
    }

    public void setPossibleHumans(LazyHumanWithActorsDataModel possibleHumans) {
        this.possibleHumans = possibleHumans;
    }
}
